﻿import com.dmotiko.akira.*
import com.dmotiko.general.*

class PortfolioMainButtonsAdvanced 
extends PortfolioMainButtons {
	
	private static var ref:PortfolioMainButtonsAdvanced;
	
	private var thumbActive:MovieClip;
	private var thumbs:Array;
	
	public function PortfolioMainButtonsAdvanced(){
		super();
		ref = this;
		thumbs = new Array();
		AkiraApp.getApp().addListener( nextPhoto, new AkiraAppEvent(AkiraAppEvent.NEXT_PHOTO, AkiraApp.getApp()) );
		AkiraApp.getApp().addListener( prevPhoto, new AkiraAppEvent(AkiraAppEvent.PREV_PHOTO, AkiraApp.getApp()) );
	}
	
	public function nextPhoto(evnt, data){
		
		for (var i:Number = 0; i < ref.thumbs.length; i++) {
			if(ref.thumbs[i].photo == AkiraApp.getApp().getPhotoSelected()){
				var nextThumb = ref.thumbs[i+1];
				//trace(ref.thumbs[i]);
				if(!nextThumb) nextThumb = ref.thumbs[0];
				ref.childChanged( nextThumb );
				nextThumb.onRollOver();
				return;
			}
		}
	}
	public function prevPhoto(evnt, data){
		
		for (var i:Number = 0; i < ref.thumbs.length; i++) {
			if(ref.thumbs[i].photo == AkiraApp.getApp().getPhotoSelected()){
				var nextThumb = ref.thumbs[i-1];
				//trace(ref.thumbs[i]);
				if(!nextThumb) nextThumb = ref.thumbs[ref.thumbs.length-1];
				ref.childChanged( nextThumb );
				nextThumb.onRollOver();
				return;
			}
		}
	}
	
	public function setData(data):Void {
		super.setData(data);
		drawThumbs();	
	}
	
	public function childLoaded(child):Void {
		return;
	}
	
	public function childChanged(child:MovieClip):Void{
		super.childChanged(child);
		thumbActive.setActive(false);
		thumbActive.onRollOut();
		thumbActive = child;
		thumbActive.setActive(true);
	}
	
	private function drawThumbs():Void {
		var nX:Number = 0;
		var nY:Number = 0;
		for(var i:Number=0; i < data.photos.length; i++){
			var photo = data.photos[i];
			var thumb = this.attachMovie("thumbnail", "mcThumbnail"+i, this.getNextHighestDepth());
			thumb.setDelay(i*150);
			thumb.setData(photo);
			thumbs.push(thumb);
			thumb.photo = photo;
			if(photo == AkiraApp.getApp().getPhotoSelected()){
				thumb.onRollOver();
				thumb.active = true;
				thumbActive = thumb;
			}
			thumb._x = nX;
			thumb._y = nY;
			if(nX > 0){
				nX = 0;
				nY += 101;
			} else {
				nX = 38;
			}
		}
		propagateEvent(new BaseClipEvent(BaseClipEvent.CHANGED));
	}
	
}